package userServlet.mysql;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;

public class Mysql {

    //1.定义需要的工具类对象
    protected Connection connection = null;
    protected PreparedStatement pps = null;
    protected ResultSet rs = null;
    protected int k = 0; //受影响的行数
    private static String url;
    private static String userName;
    private static String password;
    private static String dirverName;
    //德鲁伊
    private static DruidDataSource dds = new DruidDataSource();
    //2.加载驱动
    static {
        //德鲁伊
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        dirverName = bundle.getString("driver");
        url = bundle.getString("url");
        userName = bundle.getString("user");
        password = bundle.getString("password");
        dds.setUsername(userName);
        dds.setPassword(password);
        dds.setUrl(url);
        dds.setDriverClassName(dirverName);
    }

    //3.获得连接
    protected Connection getConnection() {
        try {
            //德鲁伊
            connection = dds.getConnection();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    //4.创建通道
    protected PreparedStatement getPps(String sql) {
        try {
            getConnection();
            pps = connection.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return pps;
    }

    //5.给占位符赋值 list中保存的是占位符所赋的值
    private void setParams(List list) {
        try {
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    pps.setObject(i+1,list.get(i));
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    //6.增删改调取的方法
    protected int update(String sql,List params) {
        try {
            getPps(sql);
            setParams(params);
            k = pps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return k;
    }

    //7.查询的时候调取一个方法
    protected ResultSet query(String sql,List list) {
        try {
            getPps(sql);
            setParams(list);
            rs = pps.executeQuery();
            return  rs;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    //8.关闭资源
    protected void closeAll(){
        try {
            if (rs != null) {
                rs.close();
            }
            if (pps != null) {
                pps.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}

